package com.fanrui.code.dao;

import java.util.Collection;
import java.util.Date;

import org.apache.ibatis.session.SqlSession;

import com.fanrui.code.db.jvmdb.JvmDao;
import com.fanrui.code.entity.LoginInfo;
import com.fanrui.code.entity.LoginInfoMapper;
import com.fanrui.code.util.SessionUtil;

public class LoginInfoDao extends JvmDao<LoginInfo, Integer> {

	// singleton
	private LoginInfoDao() {
		super(LoginInfo.class);
	}

	private static class LoginInfoDaoSingleton {
		private static LoginInfoDao instance = new LoginInfoDao();
	}

	public static LoginInfoDao getInstance() {
		return LoginInfoDaoSingleton.instance;
	}

	@Override
	protected void OtherSave4jvm(LoginInfo entity) {
		// TODO Auto-generated method stub

	}

	@Override
	protected Collection<LoginInfo> dbFindAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void dbSave(LoginInfo entity) {
		try(SqlSession session = SessionUtil.getSession()){
			LoginInfoMapper mapper = session.getMapper(LoginInfoMapper.class);
			mapper.insertSelective(entity);
			entity.setPersistKey(entity.getId());
			session.commit();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void otherDelete4Jvm(LoginInfo entity) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void dbDelete(Integer id) {
		// TODO Auto-generated method stub

	}
	
	public LoginInfo createLoginInfo(String ip, String deviceCard, int platformFlag, String appVersion, long playerId) {
		LoginInfo loginInfo = newLoginInfo(ip, deviceCard, platformFlag, appVersion, playerId);
		dbSave(loginInfo);
		save(loginInfo);
		return loginInfo;
	}
	
	private LoginInfo newLoginInfo(String ip, String deviceCard, int platformFlag, String appVersion, long playerId) {
		LoginInfo loginInfo = new LoginInfo();
		loginInfo.setLoginIp(ip);
		loginInfo.setDeviceCard(deviceCard);
		loginInfo.setPlatformFlag(platformFlag);
		loginInfo.setPlayerId(playerId);
		loginInfo.setAppVerion(appVersion);
		loginInfo.setLoginTime(new Date());
		return loginInfo;
	}

}
